package com.qiaomeng.flutter.baichuan;

import androidx.annotation.NonNull;

import com.qiaomeng.flutter.baichuan.handlers.BCApiHandler;

import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
 * BaichuanPlugin
 */
public class BaichuanPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
    private static String CHANNEL_NAME = "com.qiaomeng.flutter/baichuan";
    private MethodChannel channel;
    private BCApiHandler bcApiHandler;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), CHANNEL_NAME);
        channel.setMethodCallHandler(this);

    }


    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        String method = call.method;
        if (method.equals("debug")) {
            bcApiHandler.debug();
        } else if (method.equals("asyncInit")) {
            bcApiHandler.asyncInit(result);
        } else if (method.equals("setIsvVersion")) {
            bcApiHandler.setIsvVersion((String) call.arguments);
        } else if (method.equals("showLogin")) {
            bcApiHandler.showLogin(result);
        } else if (method.equals("logout")) {
            bcApiHandler.logout(result);
        } else if (method.equals("openByCode")) {
            String suiteCode = call.argument("suiteCode");
            Map bizParams = call.argument("bizParams");
            Map showParams = call.argument("showParams");
            Map taokeParams = call.argument("taokeParams");
            Map trackParams = call.argument("trackParams");
            bcApiHandler.openByCode(
                    suiteCode,
                    bizParams,
                    showParams,
                    taokeParams,
                    trackParams,
                    result
            );
        } else if (method.equals("openByUrl")) {
            String detailUrl = call.argument("detailUrl");
            Map showParams = call.argument("showParams");
            Map taokeParams = call.argument("taokeParams");
            Map trackParams = call.argument("trackParams");

            bcApiHandler.openByUrl(
                    detailUrl,
                    showParams,
                    taokeParams,
                    trackParams,
                    result
            );
        } else if (method.equals("registerNavigateUrl")) {
            bcApiHandler.registerNavigateUrl();
        } else if (method.equals("isLogin")) {
            bcApiHandler.isLogin(result);
        } else if (method.equals("getUserInfo")) {
            bcApiHandler.getUserInfo(result);
        } else if (method.equals("registerImage")) {
            bcApiHandler.registerImage();
        } else {
            result.notImplemented();
        }

    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        bcApiHandler = new BCApiHandler(binding.getActivity(), channel);

    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {

    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

    }

    @Override
    public void onDetachedFromActivity() {

    }
}
